<?php
require "../util/php/mysql.php" ;
require "connect.php" ;
?>
{ "vsource" : {
<?php
$liID = fnGetValue ( "tbSession", "fdSession='{$_REQUEST["session"]}'", "fdTerminalID" ) ;
if ( $liID == "" )
	$liID = fnGetValue ( "tbTerminal", "fdSession='" . $_REQUEST["session"] . "'", "id" ) ;
if ( $liID > 0 ) {
	$start = 0 + $_REQUEST["start"];
	$length = 0 + $_REQUEST["length"];
	$content = 0;
	if($_REQUEST["zone"] > 0){
		$content = fnGetValue("tysny.tbZone","id = {$_REQUEST["zone"]}","fdContentID");
	}elseif($_REQUEST["site"] > 0){
		$content = fnGetValue("tysny.tbSite","id = {$_REQUEST["site"]}","fdContentID");
	}elseif($_REQUEST["poi"] > 0){
		$content = fnGetValue("tysny.tbPoi","id = {$_REQUEST["poi"]}","fdContentID");
	}elseif($_REQUEST["content"] > 0){
		$content_count = fnGetValue("tyctn.tbContent","id = {$_REQUEST["content"] }","count(*)");
		if($content_count > 0)
			$content = $_REQUEST["content"];
	}
	if($content > 0){
		$lsFields = "";
		$lsFields = "id,fdUserID,fdText,fdCreate,fdContentID";
		$lsTable = "tysvc.tbComment";
		$lsClause = "tbComment.fdType = 0 AND tbComment.fdContentID = $content ";
		$lsOrder = "tbComment.fdCreate DESC";
		$sql = "SELECT SQL_CALC_FOUND_ROWS $lsFields FROM $lsTable WHERE $lsClause ORDER BY $lsOrder LIMIT $start,$length";
		$res_comment = mysql_exec ( $sql ) ;
		
		$res_rows = mysql_exec ( "SELECT FOUND_ROWS()" ) ;
		$row_rows = mysql_fetch_row ( $res_rows ) ;
		print "\"comments\":\"". (0 + $row_rows[0]) . "\", \"comment\":[" ;
		mysql_free_result ( $res_rows ) ;
		
		$first = true;
		while ( $row_comment = mysql_fetch_assoc ( $res_comment ) ) {
			if($first){
				print "{";
				$first = false;
			}else{
				print ",{";
			}
			 
			print "\"id\" : \"{$row_comment["id"]}\" , ";
			print "\"text\" : \"".rawurlencode($row_comment["fdText"])."\" , ";
			print "\"create\" : \"{$row_comment["fdCreate"]}\" , ";
			$rank = 0 + fnGetValue("tysvc.tbRank","fdContentID = {$row_comment["fdContentID"]} AND fdUserID = {$row_comment["fdUserID"]}","fdRank");
			print "\"rank\" : \"{$rank}\" , ";
			$agree = 0 + fnGetValue("tysvc.tbComment","fdContentID = {$row_comment["fdContentID"]} AND fdType = 2","COUNT(*)");
			print "\"agree\" : \"{$agree}\" , ";
			$userid = 0 + fnGetValue ( "tbSession", "fdSession='{$_REQUEST["session"]}'", "fdUserID" ) ;
			$useragree = 0 + fnGetValue("tysvc.tbComment","fdContentID = {$row_comment["fdContentID"]} AND fdType = 2 AND fdUserID = $userid","COUNT(*)");
			print "\"useragree\" : \"{$useragree}\" , ";
			print "\"user\" : {";
				print "\"id\" : \"{$row_comment["fdUserID"]}\" , ";
				$sql = "SELECT fdName,fdLogin FROM tysvc.tbUser WHERE tbUser.id = {$row_comment["fdUserID"]}";
				$resUser = mysql_exec ( $sql ) ;
				if ( $rowUser = mysql_fetch_assoc ( $resUser ) ) {
					  $name = $rowUser["fdName"];
					  if($name == "")
						 $name = $rowUser["fdLogin"];
				}
				print "\"name\" : \"{$name}\" ";
			print "}";
			print "}";
		}
		print "],\"ret_code\":\"200\"";
		mysql_free_result ( $res_comment ) ;
	}else{
		  ?>
		"ret_code":"201",
		"ret_error":"Empty Content"
    <?php
	}
}else{
	  ?>
		"ret_code":"200",
		"ret_error":"Failed"
    <?php
}
?>
}}